package cc.eddic.pratice.programmingparadigms.nginxlog.impl;

import cc.eddic.pratice.programmingparadigms.nginxlog.LogItem;
import cc.eddic.pratice.programmingparadigms.nginxlog.LogItemParser;
import cc.eddic.pratice.programmingparadigms.nginxlog.NginxLogItem;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Optional;


public class LogItemParserScwImpl implements LogItemParser {
    /**
     * 解析 Nginx 日志项
     *
     * @param nginxLogItem Nginx 日志项
     * @return LogItem 可能为空
     */
    @Override
    public Optional<LogItem> parse(NginxLogItem nginxLogItem) {
        String ip = nginxLogItem.getIp();
        LocalDateTime time = parseDateTime(nginxLogItem.getTime());
        Map<String, String> query = null;
        int statues = Integer.parseInt(nginxLogItem.getStatus());
        long bytes = Long.parseLong(nginxLogItem.getBytes());
        String refer = nginxLogItem.getReferer();
        String userAgent = nginxLogItem.getUserAgent();
        String method = parseMethod(nginxLogItem);
        String path = parsePath(nginxLogItem);
        String protocol = parseProtocol(nginxLogItem);
        return Optional.of(new LogItem(ip,time, method, path, query, protocol, statues, bytes, refer,userAgent));
    }

    private LocalDateTime parseDateTime(String time) {
//        return String.format(time, );

return  null;  }

    private String parseProtocol(NginxLogItem nginxLogItem) {
        return nginxLogItem.getRequest().substring(nginxLogItem.getRequest().indexOf("H"));
    }

    private String parseMethod(NginxLogItem nginxLogItem) {

        return nginxLogItem.getRequest().substring(0,nginxLogItem.getRequest().indexOf("/"));
    }


    private String parsePath(NginxLogItem nginxLogItem) {
        return nginxLogItem.getRequest().substring(nginxLogItem.getRequest().indexOf("/"),nginxLogItem.getRequest().indexOf("H"));
    }



}

